//{=========================================================================
//! @file D1.c
//! @date 05.10.2013 16:58
//! @author Andrey Drobyshev <immortalguardian1@gmail.com>
//! @ version 1.0
//!
//! @par	Conditions:
//! 		The program reads an integer non-negative number N and otputs
//!         the table of N+1 strings where number in the nth string and kth
//!         column means the number of binary words of n symbols containing
//!         k symbols '1' which cannot be in a row.
//}=========================================================================

#include <stdio.h>
#include <assert.h>

#define TableSize 50

//{========================================================================
//! Function fill_out - fills out our table with necessary values.
//!
//! @param           N   -   the number of table's strings  (except strring
//!                  number 0.
//! @param[out]      table [][N] - the table itself.
//!
//! @return     nothing
//!
//! @note       The function has assertion checks.
//}========================================================================

void fill_out ( unsigned int N, unsigned int table [][N]);

//{========================================================================
//! Function output - displays the table.
//!
//! @param           N   -   the number of table's strings (except strring
//!                  number 0.
//! @param[out]      table [][N] - the table itself.
//!
//! @return     nothing
//!
//! @note       The function has assertion checks.
//}========================================================================

void output ( unsigned int N, unsigned int table [][N]);

//{=========================================================================
//! Function main - prints welcome messages and the result, initializes
//! the table, manages all the program execution.
//!
//! @return     0 on success
//! @note       The function uses functions "fill_out", "output".
//}==========================================================================

int main ()
{
    printf ( "  #Hello!\n"
             "  #This program get your integer non-negative number N "
             "and outputs the table\n#b ( n,k) with N+1 strings where "
             "number in the nth ( n = 0, 1, 2 ...) string and\n#kth column  "
             "means the number of binary words of n symbols\n#containing "
             "k symbols '1' which cannot be in a row.\n");
    printf ( "Now enter number N: ");

    unsigned int N = 0;

    scanf ( "%u", &N);
    printf ( "\n");

    unsigned int table [TableSize][TableSize] = {};

    fill_out ( N, table);
    output ( N, table);

    return 0;
}

void fill_out ( unsigned int N, unsigned int table [][N])
{
    assert ( table != NULL);

    unsigned int i = 0, j = 0;

    for ( i = 0; i <= N+1; i++)         // Put the
        table [i][0] = 1;               // initial
    table [1][1] = 1;                   // values

    for ( i = 2; i <= N; i++)
    {
        assert ( 0 <= i && i <= N);
        assert ( 0 <= i-1 && i-1 <= N-1);
        assert ( 0 <= i-2 && i-2 <= N-1);
        for ( j = 1; j <= N-1; j++)
        {
            assert ( 0 <= j && j <= N-1);
            assert ( 0 <= j-1 && j-1 <= N-1);
            if ( !( table [i][j] = table [i-1] [j] + table [i-2][j-1]))
                break;
        }
    }
    return;
}

void output ( unsigned int N, unsigned int table [][N])
{
    assert ( table != NULL);

    unsigned int i = 0, j = 0;

    for ( i = 0; i <= N; i++)
	{
        assert ( 0 <= i && i <= N);
		while ( table [i][j])
		{
            assert ( 0 <= j && j <= N-1);
			printf( "%u ", table [i][j]);
			j++;
		}
		j = 0;
		printf ( "\n");
	}
}
